package com.jingzhi.huimiao.mineActivity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Timer;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jingzhi.huimiao.LoginActivity;
import com.jingzhi.huimiao.R;
import com.jingzhi.huimiao.mineActivity.MyChartView.Mstyle;
import com.jingzhi.huimiao.utils.DataStoreUtil;
import com.jingzhi.huimiao.utils.SQLdm;

public class PActivity extends Activity {


	MyChartView tu;
	Button BT_Add;
	Timer mTimer =new Timer();
	HashMap<Double, Double> map;
	Double key=8.0;
	Double value=0.0;
	Tools tool=new Tools();
	
	private TextView rightlv;
	private TextView wordid;
	private TextView studytime;
	private TextView tv_title;
	private LinearLayout iv_back_tomain;
	private SQLiteDatabase db;
	private Cursor rightcur;
	private int rightcount;
	private Cursor allcur;
	private int allcount;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_p);
//		BT_Add=(Button)findViewById(R.id.bt_add);
		
		rightlv = (TextView) findViewById(R.id.tv_progress_rightlv);
		wordid = (TextView) findViewById(R.id.tv_progress_wordid);
		studytime = (TextView) findViewById(R.id.tv_progress_time);
		tv_title = (TextView) findViewById(R.id.tv_title);
		iv_back_tomain = (LinearLayout) findViewById(R.id.iv_back_tomain);
		
		SQLdm s = new SQLdm();
		db = s.openDatabase(PActivity.this);
		
		rightcur = db.rawQuery("select * from word where word_right=2;", null);
		while(rightcur.moveToFirst()){
			rightcount = rightcur.getCount();
		}
		if(rightcur!=null){
			rightcur.close();
		}
		allcur = db.rawQuery("select * from word where word_right!=0;", null);
		while(allcur.moveToFirst()){
			allcount = allcur.getCount();
		}
		if(allcur!=null){
			allcur.close();
		}
		
		if(db!=null){
			db.close();
		}
		
		tv_title.setText("进步曲线");
		if(allcount!=0){
			
		rightlv.setText(rightcount/allcount+"%");
		}
		wordid.setText(allcount+"");
		Long experience = DataStoreUtil.getLong(PActivity.this, "studytime");
		studytime.setText(experience+"");
		
		iv_back_tomain.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		
		tu= (MyChartView)findViewById(R.id.menulist);
		tu.SetTuView(map,50,20," ","%",true);
		map=new HashMap<Double, Double>();
		map.put(1.0, (double) 0);
    	map.put(7.0, 50.0);
    	map.put(14.0, 64.0);
    	map.put(21.0, 81.0);
    	map.put(28.0, 32.0);
    	map.put(35.0, 72.0);
    	map.put(42.0, 52.0);
    	map.put(49.0, 59.0);
    	tu.setTotalvalue(100);
    	tu.setPjvalue(20);
    	tu.setMap(map);
//		tu.setXstr("");
//		tu.setYstr("");
		tu.setMargint(20);
		tu.setMarginb(50);
		tu.setMstyle(Mstyle.Line);
	
//		BT_Add.setOnClickListener(new click());
	}

//	class click implements OnClickListener
//	{
//
//		@Override
//		public void onClick(View v) {
//			Random rd=new Random(System.currentTimeMillis());
//			Double temp= rd.nextDouble();
//			randmap(map, temp*50);
//		}}
	
	private void randmap(HashMap<Double, Double> mp,Double d)
	{
		ArrayList<Double> dz=tool.getintfrommap(mp);
		Double[] dvz=new Double[mp.size()];
		int t=0;
		@SuppressWarnings("rawtypes")
		Set set= mp.entrySet();   
        @SuppressWarnings("rawtypes")
		Iterator iterator = set.iterator();
		 while(iterator.hasNext())
		{  
			@SuppressWarnings("rawtypes")
			Map.Entry mapentry  = (Map.Entry)iterator.next();   
			dvz[t]=(Double)mapentry.getValue();
			t+=1;
		} 
		 for(int j=0;j<dz.size()-1;j++)
		 {
			 mp.put(dz.get(j), mp.get(dz.get(j+1)));
		 }
		 mp.put((Double)dz.get(mp.size()-1), d);
		 tu.postInvalidate();
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
	        // TODO Auto-generated method stub
	        if(item.getItemId() == R.id.menu_settings)
	        {
	        	if (false == tu.isDrawingCacheEnabled()) 
	    		{  
	                tu.setDrawingCacheEnabled(true);
	            }  
	        	Bitmap bitmap = tu.getDrawingCache();  
	        	Tools tool=new Tools();
	        	try {
					Boolean b=tool.saveFile(bitmap, " ");
					if(b)
					Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();	
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
	        if(item.getItemId() == R.id.menu_ch)
	        {
	        	tu.setMstyle(Mstyle.Curve);
	        	tu.setIsylineshow(true);
	        	tu.postInvalidate();
	        }
	        if(item.getItemId() == R.id.menu_ch2)
	        {

	        	tu.setMstyle(Mstyle.Line);
	        	tu.setIsylineshow(false);
	        	tu.postInvalidate();
	        }
	        return true;
	  }

}
